#不限次数输入，选择3退出
#1、注册
#    如果有这个用户了，就不用再注册
#    name_dict = {"root":{"age":18,"passwd":"123456"}, \
#                 "admin":{"age":19, "passwd":"admin123"}}
#   输入用户名、年龄、密码
#
#2、查询
#   输入要查询的姓名，展示这个人的详细信息
#3、退出
#后续考虑：写入文件
#json库
import json

with open('dict.txt', 'r') as f:
    str1 = f.read()
    print(str1)
    if str1 == '':
        name_dict = {}
    else:
        name_dict = json.loads(str1)
while True:
    print("1.注册\n2.查询\n3.退出\n")
    n = input("请作出你的选择:")
    if n == "1":
        name = input("please your name:")
        if name in name_dict:
            print("用户已被创建\n")
        else:
            age = int(input("please your age:"))
            passwd = input("please your passwd:")
            d1 = {"age": age, "passwd": passwd}
            name_dict[name] = d1
            print(f"{name}:{name_dict[name]}\n")
    elif n == "2":
        name = input("please your name:")
        for i in name_dict:
            if i == name:
                print(f"用户的姓名：{name}")
                print(f"用户的年龄：{name_dict[name]['age']}")
                print(f"用户的密码：{name_dict[name]['passwd']}\n")
                break
        else:
            print("没有找到这个用户\n")
    elif n == "3":
        break
    else:
        print("您的输入无效，请重新输入\n")

with open('dict.txt', 'w') as file:
    file.truncate()
    file.write(json.dumps(name_dict))





